package com.financial.sys.service.bank.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.model.pojo.TAccountFlow;
import com.financial.model.pojo.TBankCard;
import com.financial.model.pojo.TUserWallet;
import com.financial.model.pojo.TWithdraw;
import com.financial.sys.mapper.user.TUserWalletsMapper;
import com.financial.sys.mapper.bank.TAccountFlowsMapper;
import com.financial.sys.mapper.bank.TBankCardsMapper;
import com.financial.sys.mapper.bank.TWithdrawsMapper;
import com.financial.sys.service.bank.TWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TWithdrawServiceImpl extends ServiceImpl<TWithdrawsMapper, TWithdraw> implements TWithdrawService {

    @Autowired
    private TUserWalletsMapper tUserWalletMapper;

    @Autowired
    private TAccountFlowsMapper accountFlowMapper;

    @Autowired
    private TBankCardsMapper tBankCardMapper;

    @Override
    public int TWithdrawAdd(TWithdraw tWithdraw) {
        String userId=tWithdraw.getUserId();
        QueryWrapper<TUserWallet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account_id",userId);
        TUserWallet tUserWallet=tUserWalletMapper.selectOne(queryWrapper);
        //查询已有
        Long availableAmount=tUserWallet.getAvailableAmount();
        //查询提出金额
        Long putAmount=tWithdraw.getAmount();
        //算提出金额加利息
        Long putAmountInterest=tWithdraw.getAmount()/100;
        //如果小于则返回出余额不足
        if((putAmount+putAmountInterest)>availableAmount){
            return 0;
        }
        //计算用户余额
        Long calculateAmount=tUserWallet.getAvailableAmount()-tWithdraw.getAmount()-(tWithdraw.getAmount()/100);
        //计算没扣利息得钱
        Long availableBalance=tUserWallet.getAvailableAmount()-tWithdraw.getAmount();
        tUserWallet.setAccountId(userId);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tUserWallet.setAvailableAmount(calculateAmount);
        //更新用户  减少可用余额
        tUserWalletMapper.updateById(tUserWallet);

        //找到该用户银行卡
        QueryWrapper<TBankCard> queryWrapper1=new QueryWrapper<>();
        System.out.println(userId+"userId");
        queryWrapper1.eq("user_id",userId);
        TBankCard tBankCard=tBankCardMapper.selectOne(queryWrapper1);
        //银行卡可用余额加提出金额
        Long balance=tBankCard.getBalance()+putAmount;
        tBankCard.setUserId(userId);
        tBankCard.setBalance(balance);
        //更新该用户银行卡可用余额
        tBankCardMapper.updateById(tBankCard);

        TAccountFlow tAccountFlow=new TAccountFlow();
        tAccountFlow.setAccountId(userId);
        tAccountFlow.setAmount(putAmount);
        //暂时为1
        tAccountFlow.setFlowType(1);
        tAccountFlow.setAvailableAmount(availableBalance);
        tAccountFlow.setFreezeAmount(tUserWallet.getFreezeAmount());
        tAccountFlow.setRemark("账户提现");
        tAccountFlow.setCreateTime(date);
        accountFlowMapper.insert(tAccountFlow);

        TAccountFlow tAccountFlow1=new TAccountFlow();
        tAccountFlow1.setAccountId(userId);
        tAccountFlow1.setAmount(putAmount);
        //暂时为1
        tAccountFlow1.setFlowType(1);
        tAccountFlow1.setAvailableAmount(calculateAmount);
        tAccountFlow1.setFreezeAmount(tUserWallet.getFreezeAmount());
        tAccountFlow1.setRemark("手续费扣减");
        tAccountFlow1.setCreateTime(date);
        accountFlowMapper.insert(tAccountFlow1);

        tWithdraw.setCreateTime(date);
        return baseMapper.insert(tWithdraw);
    }
}
